package cn.rongdao.jrkabao.ui.deposit.popup;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.mifengkong.frtools.ui.adapter.recyclerview.FRBaseRecyclerAdapter;
import com.mifengkong.frtools.inputmethod.FRView;

import java.util.List;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.deposit.SelectMonthBean;
import cn.rongdao.jrkabao.ui.deposit.adapter.SelectMonthAdapter;
import cn.rongdao.jrkabao.ui.deposit.adapter.SelectMonthViewHolder;

/**
 * Created by jack on 2017/7/31
 */

public class SelectMonthPopupUtil implements FRBaseRecyclerAdapter.OnItemClickListener<SelectMonthViewHolder> {

    private SelectMonthPopupWindow selectMonthPopupWindow;
    private SelectMonthAdapter selectMonthAdapter;
    private OnSelectMonthClickListener onSelectMonthClickListener;

    public SelectMonthPopupUtil(Activity activity) {
        View itemsView = FRView.inflateLayout(activity, R.layout.layout_select_month_popup);
        selectMonthPopupWindow = new SelectMonthPopupWindow(activity);
        selectMonthPopupWindow.setMenuItems(itemsView);
        RecyclerView recyclerView = FRView.findView(itemsView, R.id.lsmp_recyclerview);
        selectMonthAdapter = new SelectMonthAdapter(activity, this);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        recyclerView.setAdapter(selectMonthAdapter);
        recyclerView.setFocusable(true);
        recyclerView.setFocusableInTouchMode(true);
    }

    public void setSelectMonthBeanList(List<SelectMonthBean> selectMonthBeanList) {
        if (selectMonthBeanList.size() > 0) {
            SelectMonthBean selectMonthBean = selectMonthBeanList.get(selectMonthBeanList.size() - 1);
            selectMonthBean.setLastItem(true);
            selectMonthBeanList.set(selectMonthBeanList.size() - 1, selectMonthBean);
        }
        selectMonthAdapter.setDataList(selectMonthBeanList);
    }

    @Override
    public void onItemClick(SelectMonthViewHolder selectMonthViewHolder, int position) {
        if (!selectMonthAdapter.getDataList().get(position).isSelected()) {
            if (null != onSelectMonthClickListener) {
                onSelectMonthClickListener.getPosition(position);
            }
            selectMonthAdapter.setAllUnSelect();
            selectMonthAdapter.getDataList().get(position).setSelected(true);
            selectMonthAdapter.notifyDataSetChanged();
        }
        selectMonthPopupWindow.dismiss();
    }

    public void setOnSelectMonthClickListener(OnSelectMonthClickListener onSelectMonthClickListener) {
        this.onSelectMonthClickListener = onSelectMonthClickListener;
    }

    public void showPopupWindow(View v) {
        selectMonthPopupWindow.showPopupWindow(v);
    }

    public interface OnSelectMonthClickListener {
        void getPosition(int position);
    }
}